package servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;
import managers.JSonManager;
import structures.ADsList;

public class GetSellerADsServlet extends HttpServlet {

	private static final long serialVersionUID = -6486280909295952154L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		String idSeller, adType , jsonResult;
		ADsList adsList;
		JSonManager jSonManager;
		jSonManager = new JSonManager();
		DatabaseManager databaseManager;
		databaseManager = new DatabaseManager();
		idSeller = request.getParameter("idSeller");
		//adType can be: "All", "Bought", "Available" or "Sold"
		adType = request.getParameter("adType");
         try {
        	databaseManager.connect();
        	adsList = databaseManager.getADs(idSeller, adType);
        	jsonResult = jSonManager.generateJSON(adsList);
        	response.setContentType("application/json");
    		response.setCharacterEncoding("UTF-8");
			response.getWriter().write(jsonResult);
			databaseManager.closeConnection();
	        
        } catch (Exception e) {
			e.printStackTrace();
		}
    }
	
}
